<?php

/**
 * This is the model class for table "coupon".
 *
 * The followings are the available columns in table 'coupon':
 * @property string $id
 * @property string $user_id
 * @property string $partner_id
 * @property string $team_id
 * @property string $order_id
 * @property string $secret
 * @property string $consume
 * @property string $expire_time
 * @property string $consume_time
 * @property string $create_time
 *
 * The followings are the available model relations:
 */
class Coupon extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Coupon the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'coupon';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		 	array('id,secret', 'required'),
			array('id', 'length', 'max'=>12),
			array('user_id, partner_id, team_id, order_id, secret, expire_time, consume_time, create_time', 'length', 'max'=>10),
			array('consume', 'length', 'max'=>1),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, partner_id, team_id, order_id, secret, consume, expire_time, consume_time, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
					'name'=>array(self::BELONGS_TO,'User','user_id'),
					'partnername'=>array(self::BELONGS_TO,'Partner','partner_id'),
					'teamname'=>array(self::BELONGS_TO,'Team','team_id'),
					'ordername'=>array(self::BELONGS_TO,'Order','order_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => '客户ID',
			'partner_id' => '商户ID',
			'team_id' => '团购ID',
			'order_id' => '订单ID',
			'secret' => '密码',
			'consume' => '是否消费',
			'expire_time' => '到期时间',
			'consume_time' => '消费时间',
			'create_time' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('partner_id',$this->partner_id,true);
		$criteria->compare('team_id',$this->team_id,true);
		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('secret',$this->secret,true);
		$criteria->compare('consume',$this->consume,true);
		$criteria->compare('expire_time',$this->expire_time,true);
		$criteria->compare('consume_time',$this->consume_time,true);
		$criteria->compare('create_time',$this->create_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	  protected function beforeSave()
{
    if(parent::beforeSave())
    {
        if($this->isNewRecord)
        {
            $this->create_time=time();

        }

        return true;
    }

}
}